﻿Public Class FrmSanPham

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Sub Them()
        Dim sp As SanPhamDTO = New SanPhamDTO()
        sp.TenSanPham = txtTenSP.Text
        sp.DonViTinh = txtDonVT.Text
        sp.Gia = txtGia.Text
        sp.MaNhaCC = cmbNhaCC.SelectedValue
        sp.SoLuong = txtSoLuong.Text

        SanPhamDAO.Insert(sp)
        LoadLenLuoi()
    End Sub

    Sub Xoa()
        Dim sp As SanPhamDTO = New SanPhamDTO()
        sp.MaSanPham = Convert.ToInt32(txtMaSP.Text)
        SanPhamDAO.Delete(sp)
        LoadLenLuoi()

    End Sub
    Sub Sua()
        Dim sp As SanPhamDTO = New SanPhamDTO()
        sp.MaSanPham = txtMaSP.Text
        sp.TenSanPham = txtTenSP.Text
        sp.DonViTinh = txtDonVT.Text
        sp.Gia = txtGia.Text
        sp.MaNhaCC = cmbNhaCC.SelectedValue
        sp.SoLuong = txtSoLuong.Text

        SanPhamDAO.Update(sp)
        LoadLenLuoi()
    End Sub

    Sub LoadLenLuoi()
        Dim ll As DataTable
        ll = SanPhamDAO.SelectAll()
        dgvSanPham.DataSource = ll

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Them()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Xoa()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Sua()
    End Sub

    Private Sub dgvSanPham_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvSanPham.SelectionChanged
        HienThiChinhSua()
    End Sub

    Sub HienThiChinhSua()
        If (dgvSanPham.SelectedRows.Count > 0) Then
            txtMaSP.Text = dgvSanPham.SelectedRows(0).Cells(0).Value.ToString()
            txtTenSP.Text = dgvSanPham.SelectedRows(0).Cells(1).Value.ToString()
            txtDonVT.Text = dgvSanPham.SelectedRows(0).Cells(2).Value.ToString()
            txtGia.Text = dgvSanPham.SelectedRows(0).Cells(3).Value.ToString()
            cmbNhaCC.Text = dgvSanPham.SelectedRows(0).Cells(4).Value.ToString()
            txtSoLuong.Text = dgvSanPham.SelectedRows(0).Cells(5).Value.ToString()
        End If
    End Sub

    Private Sub dgvSanPham_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSanPham.CellContentClick
        LoadLenLuoi()
    End Sub

    Sub loadcmb()
        cmbNhaCC.DataSource = NhaCungCapDAO.SelectAll()
        cmbNhaCC.DisplayMember = "TenNhaCC"
        cmbNhaCC.ValueMember = "IDNhaCC"
    End Sub

    Private Sub FrmSanPham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadcmb()
        LoadLenLuoi()
    End Sub

    Private Sub btnNhapMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapMoi.Click
        txtMaSP.Text = ""
        txtTenSP.Text = ""
        txtDonVT.Text = ""
        txtGia.Text = ""
        txtSoLuong.Text = ""
    End Sub
End Class